<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Master_kota extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
        $this->load->model('kota_model');
		$this->cek_login();
	}
	
	
    function cek_login(){
		$id_hak = $this->session->userdata('id_hak');
		if($id_hak != 1 || $id_hak == ''){
			redirect ('login');
		}
		
	}

    //load Grid
    function index() {
        $colModel['no'] = array('No', 30, TRUE, 'center', 0);
        $colModel['Nama_Kota'] = array('Nama Kota', 200, TRUE, 'center', 1);
        $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);

        $gridParams = array(
            'width' => 'auto',
            'height' => 300,
            'rp' => 10,
            'rpOptions' => '[5,10,15,20,25,40]',
            'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
            'blockOpacity' => 0.5,
            'title' => 'Master Kota',
            'showTableToggleBtn' => true
        );

        //$buttons[] = array('Tambah','add','test');
        //$buttons[] = array('separator');

        $buttons[] = array('Tambah', 'add', 'spt_js');
        $buttons[] = array('separator');
        $buttons[] = array('Hapus', 'delete', 'spt_js');

        // mengambil data dari file controler ajax pada method grid_berkas
        $grid_js = build_grid_js('flex1', site_url("lisensi/master_kota/grid_kota"), $colModel, 'Nama_Kota', 'asc', $gridParams, $buttons);


        //additional JS
        $data['added_js'] =
                "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/lisensi/master_kota/form_kota'; 
			}
			
			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' data kota?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/lisensi/master_kota/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					} 
				}
				
		} 
		</script>
		";

        $data['js_grid'] = $grid_js;
        //remdering view
        $data['title'] = 'Master Kota';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/grid_kota', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    function grid_kota() {
        $valid_fields = array('Nama_Kota');
        $this->flexigrid->validate_post('Nama_Kota', 'asc', $valid_fields);
        $records = $this->kota_model->get_data_kota();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
            $link_edit = "<a href='" . base_url() . "index.php/lisensi/master_kota/edit/" . $row->ID_KOTA. "'>";
            $image_edit = "" . base_url() . "images/grid/edit.png";
            $record_items[] = array(
                $row->ID_KOTA,
                $no = $no + 1,
                $row->NAMA_KOTA,
                '' . $link_edit . '<img border=\'0\' src=\'' . $image_edit . '\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    public function form_kota() {
        $data['status'] = 'new';
        $data['title'] = 'Tambah Kota';

        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/form_kota', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    public function insert_kota() {
        $data = array(
            'NAMA_KOTA' => $this->input->post('nama_kota'),
            'STATUS' => 1
        );
        $this->kota_model->insert_kota($data);
        redirect('lisensi/master_kota');
    }

    public function edit($id) {
        $data['status'] = 'edit';
        $record_peserta = $this->kota_model->selectone($id);
        foreach ($record_peserta->result() as $peserta) {
            $data['id_kota'] = $peserta->ID_KOTA;
            $data['nama_kota'] = $peserta->NAMA_KOTA;
        }
        $data['title'] = 'Edit Kota';
        $this->template->write_view('header', 'template/header', $data);
        $this->template->write_view('content', 'lisensi/form_kota', $data);
        $this->template->write_view('footer', 'template/footer', $data);
        $this->template->render();
    }

    //update peserta
    public function update_kota($id) {
        $data = array(
            'NAMA_KOTA' => $this->input->post('nama_kota'),
        );
        $this->kota_model->update_kota($id, $data);
        redirect('lisensi/master_kota');
    }

    public function delete($id) {
        $spt_ids_post_array = split(",", $this->input->post('items'));
        foreach ($spt_ids_post_array as $index => $id) {
            if (isset($id) && $id != '') {
                $this->kota_model->delete($id);
            }//end if
        }//end foreach
    }

}

/* End of file pegawai.php */
/* Location: ./application/controllers/pegawai.php */